/*
 * Syntelos 'SX'
 * Copyright (C) 1998, 2007  John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package syntelos.sx.methods.s;

import syntelos.sx.Response;
import syntelos.sx.Request;
import syntelos.sx.Server;
import syntelos.sx.Socket;

import alto.io.Output;
import alto.io.u.Objmap;
import alto.lang.Buffer;
import alto.lang.Component;
import alto.lang.Type;
import alto.sec.Keys;
import alto.sec.x509.X509Certificate;


/**
 * Generally, request paths have the form
 * <pre>
 * /s/{identifier}.{fext}
 * </pre>
 * 
 * @since 1.6
 */
public abstract class Abstract
    extends syntelos.sx.Response
{
    protected final static String IdentifierPrefix = "/s/";
    protected final static int IdentifierPrefixLength = IdentifierPrefix.length();


    /**
     * Requires <code>"/s/"</code> prefix and filename extension
     * suffix.
     */
    public final static String IdentifierFrom(Request request){
        String path = request.getPath();
        if (null != path){
            if (path.startsWith(IdentifierPrefix)){
                String identifier = path.substring(IdentifierPrefixLength);
                int idx = identifier.lastIndexOf('.');
                if (0 < idx){
                    identifier = identifier.substring(0,idx);
                    if (Component.Tools.IsName(identifier))
                        return identifier;
                }
            }
        }
        return null;
    }
    /**
     * Requires <code>"/s/"</code> prefix with optional filename
     * extension suffix.
     */
    public final static String IdentifierFor(Request request){
        String path = request.getPath();
        if (null != path){
            if (path.startsWith(IdentifierPrefix)){
                String identifier = path.substring(IdentifierPrefixLength);
                int idx = identifier.lastIndexOf('.');
                if (0 < idx){
                    identifier = identifier.substring(0,idx);
                }
                if (Component.Tools.IsName(identifier))
                    return identifier;
            }
        }
        return null;
    }


    protected Abstract(){
        super();
    }


    public void respond(Request req)
        throws java.io.IOException,
               java.lang.InterruptedException
    {
        this.setHeader(syntelos.sx.Headers.Method.SXS);
    }
}
